package com.lul15834.orderfood.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.core.content.ContextCompat;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.until.FileImgUntil;

//链接数据鲁
public class DBUntil extends SQLiteOpenHelper {

    private static final int version=37;//版本号，每次修改表结构都要加一，否则不生效
    private static final String dataBaseName="db_takeaway.db";
    private Context context;
    public static SQLiteDatabase con;//链接数据库的链接，通过它可以操作数据库
    public DBUntil( Context context) {
        super(context, dataBaseName, null, version,null);
        this.context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        创建数据库
        db.execSQL("PRAGMA foreign_keys = false");//让主外键失效
        //创建商家表
        db.execSQL("drop table if exists d_business");//如果表存在则删除
        db.execSQL("create table d_business(s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_describe varchar(200)," +
                "s_type varchar(20)," +
                "s_img varchar(255))");//储存图片路径
        db.execSQL("INSERT INTO d_business(s_id, s_pwd, s_name, s_describe, s_type, s_img) " +
                                  "VALUES (?, ?, ?, ?, ?, ?)",
        new Object[]{"root","123456","ccnb杂货店","卖一些小玩具","玩具店","/storage/emulated/0/Pictures/44a3d827c3ff4673a9cd171774a8b36f.png"});
        db.execSQL("INSERT INTO d_business(s_id, s_pwd, s_name, s_describe, s_type, s_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?)",
        new Object[]{"test","123456","小杨杂货店","卖一些小玩具","玩具店","/storage/emulated/0/Pictures/44a3d827c3ff4673a9cd171774a8b36f.png"});
        //创建用户表
        String path= FileImgUntil.getImgName();//图片存储路径名字
        Drawable defaultDrawable= ContextCompat.getDrawable(context, R.drawable.upimg);
        Bitmap bitmapDef =((BitmapDrawable) defaultDrawable).getBitmap();//获取图片的二进制文件
        FileImgUntil.saveBitmapAsync(bitmapDef, path);//保存图片
        db.execSQL("drop table if exists d_user");//如果表存在则删除
        db.execSQL("create table d_user(s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_sex varchar(20)," +
                "s_address varchar(200)," +
                "s_phone varchar(20)," +
                "s_img varchar(255))");
        db.execSQL("INSERT INTO d_user(s_id, s_pwd, s_name, s_sex,s_address, s_phone, s_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"admin","123456","小杨用户","男","北京市","123456789",path});
        //食物表
        db.execSQL("drop table if exists d_food");
        db.execSQL("create table d_food (s_food_id VARCHAR(20) PRIMARY KEY," +
                "s_business_id VARCHAR(20)," + // 店铺ID
                "s_food_name VARCHAR(20)," + // 食物名字
                "s_food_des VARCHAR(200)," + // 食物描述
                "s_food_price VARCHAR(20)," + // 食物价格
                "s_food_img VARCHAR(255))"); // 食物图片

        String foodImg = FileImgUntil.getImgName(); // 图片存储路径名字
        FileImgUntil.saveSystemImgToPath(context, R.drawable.mlt, foodImg);

        db.execSQL("INSERT INTO d_food (s_food_id, s_business_id, s_food_name, s_food_des, s_food_price, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?)",
                new Object[]{"1", "root", "东北麻辣烫", "不吃就会后悔", "19.88", foodImg});

        String foodImg1 = FileImgUntil.getImgName(); // 图片存储路径名字
        FileImgUntil.saveSystemImgToPath(context, R.drawable.klm, foodImg1);

        db.execSQL("INSERT INTO d_food (s_food_id, s_business_id, s_food_name, s_food_des, s_food_price, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?)",
                new Object[]{"2", "root", "东北烤冷面", "不吃就会后悔", "19.86", foodImg1});
        db.execSQL("INSERT INTO d_food (s_food_id, s_business_id, s_food_name, s_food_des, s_food_price, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?)",
                new Object[]{"3", "root", "东北麻辣烫", "不吃就会后悔", "19.88", foodImg});
        db.execSQL("INSERT INTO d_food (s_food_id, s_business_id, s_food_name, s_food_des, s_food_price, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?)",
                new Object[]{"4", "root", "东北麻辣烫", "不吃就会后悔", "19.88", foodImg});
        db.execSQL("INSERT INTO d_food (s_food_id, s_business_id, s_food_name, s_food_des, s_food_price, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?)",
                new Object[]{"5", "root", "东北麻辣烫", "不吃就会后悔", "19.88", foodImg});
        //订单表 订单编号，订单时间，购买人id，商家id，订单详情,收货地址，商品详情
        db.execSQL("drop table if exists d_orders");
        db.execSQL("create table d_orders (s_order_id VARCHAR(20) PRIMARY KEY," +
                "s_order_time VARCHAR(30)," + // 订单时间
                "s_business_id VARCHAR(20)," + // 商家id
                "s_user_id VARCHAR(20)," + //购买人id
                "s_order_details_id VARCHAR(30)," +//订单id
                "s_order_sta VARCHAR(30)," +//三钟状态，1.商家未处理，2.商家取消订单买家取消订单，取消订单，3.完成的订单
                "s_order_address VARCHAR(200))"); // 收货地址

        db.execSQL("INSERT INTO d_orders (s_order_id, s_order_time, s_business_id, s_user_id,s_order_details_id,s_order_sta, s_order_address) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"1", "2025-04-07 12:36:54", "root","admin",  "1","1","小明-1111-种花家，兔子区，1111"});
        db.execSQL("INSERT INTO d_orders (s_order_id, s_order_time, s_business_id, s_user_id,s_order_details_id,s_order_sta, s_order_address) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"2", "2025-04-09 12:36:54", "root","admin", "2","2", "小明-1111-种花家，兔子区，1111"});
        db.execSQL("INSERT INTO d_orders (s_order_id, s_order_time, s_business_id, s_user_id,s_order_details_id,s_order_sta, s_order_address) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"3", "2025-04-08 12:36:54", "root","admin", "3", "3","小明-1111-种花家，兔子区，1111"});

        //订单详情表
        db.execSQL("drop table if exists d_order_details");
        db.execSQL("create table d_order_details (s_details_id VARCHAR(30)," +
                "s_food_id VARCHAR(20)," + // 店铺ID
                "s_food_name VARCHAR(20)," + // 食物名字
                "s_food_des VARCHAR(200)," + // 食物描述
                "s_food_price VARCHAR(20)," + // 食物价格
                "s_food_num VARCHAR(200)," +//食物数量
                "s_food_img VARCHAR(255))"); // 食物图片

        db.execSQL("INSERT INTO d_order_details (s_details_id,s_food_id, s_food_name, s_food_des, s_food_price, s_food_num, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"1", "1", "东北麻辣烫", "不吃就会后悔", "19.88","5", foodImg});
        db.execSQL("INSERT INTO d_order_details (s_details_id,s_food_id, s_food_name, s_food_des, s_food_price, s_food_num, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"1", "2", "东北烤冷面", "不吃就会后悔", "19.86","10", foodImg1});
        db.execSQL("INSERT INTO d_order_details (s_details_id,s_food_id, s_food_name, s_food_des, s_food_price, s_food_num, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"2", "1", "东北麻辣烫", "不吃就会后悔", "19.88","5", foodImg});
        db.execSQL("INSERT INTO d_order_details (s_details_id,s_food_id, s_food_name, s_food_des, s_food_price, s_food_num, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"2", "2", "东北烤冷面", "不吃就会后悔", "19.86","10", foodImg1});
        db.execSQL("INSERT INTO d_order_details (s_details_id,s_food_id, s_food_name, s_food_des, s_food_price, s_food_num, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"3", "2", "东北烤冷面", "不吃就会后悔", "19.86","110", foodImg1});
        //评论表
        db.execSQL("drop table if exists d_comments");//如果表存在则删除
        db.execSQL("create table d_comments(s_comment_id varchar(20) primary key," +//评论id
                "s_comment_user_id varchar(20)," +//评论用户id
                "s_comment_business_id varchar(20)," +//评论商家id
                "s_comment_content varchar(200)," +//评论内容
                "s_comment_time varchar(20)," +//评论时间
                "s_comment_score varchar(20),"+//评论分数
                "s_comment_img varchar(255))");////评论图片 可有可无
        db.execSQL("INSERT INTO d_comments (s_comment_id,s_comment_user_id, s_comment_business_id, s_comment_content, s_comment_time, s_comment_score,s_comment_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"1", "admin", "root", "很好吃", "2025--04-08 10:15","5", foodImg1});
        db.execSQL("INSERT INTO d_comments (s_comment_id,s_comment_user_id, s_comment_business_id, s_comment_content, s_comment_time, s_comment_score,s_comment_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"2", "admin", "root", "一般般", "2025--04-09 10:15","1", foodImg});
        db.execSQL("INSERT INTO d_comments (s_comment_id,s_comment_user_id, s_comment_business_id, s_comment_content, s_comment_time, s_comment_score,s_comment_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"3", "admin", "root", "吃了一次还想顶", "2025--04-06 10:15","3", foodImg});
        db.execSQL("INSERT INTO d_comments (s_comment_id,s_comment_user_id, s_comment_business_id, s_comment_content, s_comment_time, s_comment_score,s_comment_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{"4", "admin", "root", "吃了一次还想顶", "2025--04-06 10:15","3", foodImg});
        //评论表
        db.execSQL("drop table if exists d_address");//如果表存在则删除
        db.execSQL("create table d_address(s_address_id varchar(20) primary key," +//评论id
                "s_user_id varchar(20)," +//用户账号
                "s_user_name varchar(20)," +//用户名称
                "s_user_address varchar(200)," +//用户地址
                "s_user_phone varchar(255))");//用户联系方式
        db.execSQL("INSERT INTO d_address (s_address_id,s_user_id, s_user_name, s_user_address, s_user_phone) " +
                        "VALUES (?, ?, ?, ?, ?)",
                new Object[]{"1", "admin", "小杨先生", "天津市-唐芳区111号", "13563326133"});
        db.execSQL("INSERT INTO d_address (s_address_id,s_user_id, s_user_name, s_user_address, s_user_phone) " +
                        "VALUES (?, ?, ?, ?, ?)",
                new Object[]{"2", "admin", "小明先生", "佛山市-南海区222号", "13563326133"});
        db.execSQL("INSERT INTO d_address (s_address_id,s_user_id, s_user_name, s_user_address, s_user_phone) " +
                        "VALUES (?, ?, ?, ?, ?)",
                new Object[]{"3", "admin", "小名先生", "广州市-番禺区333号", "13563326133"});
        db.execSQL("INSERT INTO d_address (s_address_id,s_user_id, s_user_name, s_user_address, s_user_phone) " +
                        "VALUES (?, ?, ?, ?, ?)",
                new Object[]{"4", "admin", "小光先生", "东莞市-虎门区444号", "13563326133"});
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }
}
